<?php
// +----------------------------------------------------------------------
// | Ht.Memorial
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2017 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------

namespace app\client\controller;

use controller\BasicClient;
use service\DataService;
use service\ToolsService;
use service\LogService;
use think\Db;

/**
 * 图片管理
 * Class Article
 * @package app\client\controller
 */
class Image extends BasicClient
{

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        // $this->user = session('c_user');
        // $this->uid = $this->user['create_by'];
    }

    /**
     * 相册表
     * @var string
     */
    protected $table = 'photo';

    /**
     * 相册管理
     */
    public function index()
    {
        $this->title='相册管理';
        $Map=[
            'create_by'=>$this->uid,
            'is_deleted'=>0
        ];
        $db = Db::name($this->table)->where($Map)->order('id asc');
        // halt($db);
        return parent::_list($db, false);
    }

    /**
     * 添加相册
     */
    public function add()
    {
        $this->title='添加相册';
        if($this->request->isPost()){
        	$post=$this->request->post();
        	if($post['id']){
        		//编辑
        		$where=array(
        			'create_by'=>$this->uid,
        			'photo_name'=>$post['photo_name'],
        			'id'=>array('neq',$post['id']),
        			'is_deleted'=>0
        		);
        	}else{
        		//新增
        		$where=array(
        			'create_by'=>$this->uid,
        			'photo_name'=>$post['photo_name'],
        			'is_deleted'=>0
        		);
        	}
        	$result=Db::name($this->table)->where($where)->find();
        	if($result){
        		$this->error('相册名已存在');
        	}
        }
        return $this->_form($this->table,'form');
    }




    /**
     * 编辑相册
     */
    public function edit()
    {
        $this->title='编辑相册';
        return $this->add();
    }

    /**
     * 相册删除
     */
    public function delPhoto()
    {
        if (DataService::update($this->table)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 相册照片列表
     */
    public function picture()
    {
        $list = Db::name('PhotoPic')->where('photo_id',$this->request->get('id'))->select();
        $html = '';
        foreach ($list as $key => $value){
            $html .= '<div id="img_'.$value['id'].'">';
            $html .= '<img data-tips-image src="'.$value['photo_picture_url'].'"/>';
            $html .= '<br><a href="JavaScript:void(0)" onclick="del_img('.$value['id'].')">删除</a>';
            $html .= ' </div>';
        }
        return $this->fetch('',['id'=>$this->request->get('id'),'html'=>$html]);
    }

    /**
     * 相册照片添加
     */
    public function add_photo()
    {
        $html = '';
        foreach (explode("|",input('value')) as $key => $value){
            if ($result = Db::name('PhotoPic')->insert(['photo_id'=>input('id'),'photo_picture_url'=>$value],$replace = false, $getLastInsID = true)) {
                $html .= '<div id="img_'.$result.'">';
                $html .= '<img data-tips-image src="'.$value.'"/>';
                $html .= '<br><a href="JavaScript:void(0)" onclick="del_img('.$result.')">删除</a>';
                $html .= ' </div>';
            }
        }
        if ($html){
            die($html);
        } else {
            die('0');
        }
    }

    /**
     * 相册照片删除
     */
    public function DelPicture()
    {
        $table = "PhotoPic";
        if (DataService::update($table)) {
            die('删除成功！');
        }
        die("删除失败，请稍候再试！");
    }

    /**
     * 删除相册
     */
    public function del() {
        if (DataService::update($this->table)) {
            LogService::write('系统管理', '相册删除成功');
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }
}